


import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.JTextComponent;
import javax.swing.undo.UndoManager;


public class Notepad extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	public JTextPane textPane;
	private JMenuBar menuBar = new JMenuBar();
	public JMenu file = new JMenu("File");
	private JMenu edit = new JMenu("Edit");
	private JMenu format = new JMenu("Format");
	private JMenu help = new JMenu("Help");

	public Actions actions = new Actions();
	
	//#if ${Redo} == "T" or ${Undo} == "T"
	public UndoManager undo = new UndoManager();
	//#endif
	public static void main(String[] args) {
		new Notepad().setVisible(true);
	}

	public Notepad() {
		super();
		{
			{
				setTitle("Untitled - JAVA Notepad SPL1");
				setSize(800, 600);
				this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
				Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
				this.setLocation((screenSize.width-this.getWidth())/2,(screenSize.height-this.getHeight())/2);
			}
			{
				Container container = getContentPane();
				textPane = new JTextPane();
				container.add(textPane);
				container.add(new JScrollPane(textPane));

				this.montaMenu();
			}
			//#if ${Redo} == "T" or ${Undo} == "T"
			{
				getTextComponent().getDocument().addUndoableEditListener( new UndoableEditListener() {
					public void undoableEditHappened( UndoableEditEvent e ) {
						//Remember the edit and update the menus
						undo.addEdit( e.getEdit() );
						//undoAction.update();
						//redoAction.update();
					}
				} );
			}
			//#endif
		}
	}

	public void montaMenu() {

		//Menu File
		//#if ${New} == "T"
		JMenuItem itemNew = new JMenuItem("New");
		itemNew.addActionListener(this);
		//#endif
		//#if ${Open} == "T"
		JMenuItem itemOpen = new JMenuItem("Open...");
		itemOpen.addActionListener(this);
		//#endif
		//#if ${Print} == "T"
		JMenuItem itemPrint = new JMenuItem("Print");
		itemPrint.addActionListener(this);
		//#endif
		//#if ${Save_Normal} == "T"
		JMenuItem itemSave = new JMenuItem("Save");
		itemSave.addActionListener(this);
		//#endif
		//#if ${Save_As} == "T"
		JMenuItem itemSave_As = new JMenuItem("Save As");
		itemSave_As.addActionListener(this);
		//#endif
		//#if ${Exit} == "T"
		JMenuItem itemExit = new JMenuItem("Exit");
		itemExit.addActionListener(this);
		//#endif
		
		//#if ${New} == "T"
		file.add(itemNew);
		//#endif
		//#if ${Open} == "T"
		file.add(itemOpen);
		//#endif
		//#if ${New} == "T" or ${Open} == "T"
		file.addSeparator();
		//#endif
		//#if ${Print} == "T"
		file.add(itemPrint);
		file.addSeparator();
		//#endif
		//#if ${Save_Normal} == "T"
		file.add(itemSave);
		//#endif
		//#if ${Save_As} == "T"
		file.add(itemSave_As);
		//#endif
		//#if ${Exit} == "T"
		file.addSeparator();
		file.add(itemExit);
		//#endif

		// Menu Edit
		//#if ${Copy} == "T"
		JMenuItem itemCopy = new JMenuItem("Copy");
		itemCopy.addActionListener(this);
		//#endif
		//#if ${Cut} == "T"
		JMenuItem itemCut = new JMenuItem("Cut");
		itemCut.addActionListener(this);
		//#endif
		//#if ${Paste} == "T"
		JMenuItem itemPaste = new JMenuItem("Paste");
		itemPaste.addActionListener(this);
		//#endif
		//#if ${Select_All} == "T"
		JMenuItem itemSelect_All = new JMenuItem("Select All");
		itemSelect_All.addActionListener(this);
		//#endif
		//#if ${Find} == "T"
		JMenuItem itemFind = new JMenuItem("Find");
		itemFind.addActionListener(this);
		//#endif
		//#if ${Find_Next} == "T"
		JMenuItem itemFind_Next = new JMenuItem("Find Next");
		itemFind_Next.addActionListener(this);
		//#endif
		//#if ${Redo} == "T"
		JMenuItem itemRedo = new JMenuItem("Redo");
		itemRedo.addActionListener(this);
		//#endif
		//#if ${Undo} == "T"
		JMenuItem itemUndo = new JMenuItem("Undo");
		itemUndo.addActionListener(this);
		//#endif

		//#if ${Copy} == "T"
		edit.add(itemCopy);
		//#endif
		//#if ${Cut} == "T"
		edit.add(itemCut);
		//#endif
		//#if ${Paste} == "T"
		edit.add(itemPaste);
		//#endif
		//#if ${Copy} == "T"
		edit.addSeparator();
		//#endif
		//#if ${Select_All} == "T"
		edit.add(itemSelect_All);
		edit.addSeparator();
		//#endif
		//#if ${Find} == "T"
		edit.add(itemFind);
		//#endif
		//#if ${Find_Next} == "T"
		edit.add(itemFind_Next);
		//#endif
		//#if ${Find_Next} == "T" or ${Undo} == "T"
		edit.addSeparator();
		//#endif
		//#if ${Redo} == "T"
		edit.add(itemRedo);
		//#endif
		//#if ${Undo} == "T"
		edit.add(itemUndo);
		//#endif

		// Menu Format
		//#if ${Font} == "T"
		JMenuItem itemSet_Font = new JMenuItem("Set Font");
		itemSet_Font.addActionListener(this);
		
		format.add(itemSet_Font);
		//#endif

		// Menu Help
		//#if ${About} == "T"
		JMenuItem itemAbout = new JMenuItem("About");
		itemAbout.addActionListener(this);
		
		help.add(itemAbout);
		//#endif
		
		
		menuBar.add(file);
		//#if ${Copy} == "T" 
		menuBar.add(edit);
		//#endif		
		menuBar.add(format);
		//#if ${About} == "T"
		menuBar.add(help);
		//#endif

		setJMenuBar(menuBar);
	}

	public void actionPerformed(ActionEvent evt) {
		if (evt.getActionCommand().equals("New")) {
			actions.newFile(textPane, this);
		}
		//#if ${Open} == "T"
		else if (evt.getActionCommand().equals("Open...")) {
			actions.open(textPane, this);		
		}
		//#endif
		//#if ${Print} == "T" 
		else if (evt.getActionCommand().equals("Print")) {
			System.out.println("Print");
		}
		//#endif
		//#if ${Save_Normal} == "T"
		else if (evt.getActionCommand().equals("Save")) {
			actions.save(textPane);
		}
		//#endif
		//#if ${Save_As} == "T"
		else if (evt.getActionCommand().equals("Save As")) {
			String fileName = actions.save_as(textPane);
			setTitle(fileName+" - JAVA Notepad SPL1");
		}
		//#endif
		//#if ${Exit} == "T"
		else if (evt.getActionCommand().equals("Exit")) {
			actions.exit(textPane);
		}
		//#endif
		//#if ${Copy} == "T"
		else if (evt.getActionCommand().equals("Copy")) {
			actions.copy(textPane);
		}
		//#endif
		//#if ${Cut} == "T"
		else if (evt.getActionCommand().equals("Cut")) {
			actions.cut(textPane);
		}
		//#endif
		//#if ${Paste} == "T"
		else if (evt.getActionCommand().equals("Paste")) {
			actions.paste(textPane);
		}
		//#endif
		//#if ${Select_All} == "T"
		else if (evt.getActionCommand().equals("Select All")) {
			actions.select_all(textPane);
		}
		//#endif
		//#if ${Find} == "T"
		else if (evt.getActionCommand().equals("Find")) {
			actions.find(textPane);
		}
		//#endif
		//#if ${Find_Next} == "T"
		else if (evt.getActionCommand().equals("Find Next")) {
			actions.find_next(textPane);
		}
		//#endif
		//#if ${Redo} == "T"
		else if (evt.getActionCommand().equals("Redo")) {
			actions.redo(undo);
		}
		//#endif
		//#if ${Undo} == "T"
		else if (evt.getActionCommand().equals("Undo")) {
			actions.undo(undo);
		}
		//#endif
		//#if ${Font} == "T"
		else if (evt.getActionCommand().equals("Set Font")) {
			actions.fonT(textPane);
		}
		//#endif
		//#if ${About} == "T"
		else if (evt.getActionCommand().equals("About")) {
			actions.about();
		}
		//#endif
	}
	
	public JTextComponent getTextComponent() {
		return textPane;
	}
}
